package com.qf.fmall2203.comments.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall2203.comments.entity.CommentsVo;
import com.qf.fmall2203.comments.entity.ProductComments;
import com.qf.fmall2203.comments.mapper.ProductCommentsMapper;
import com.qf.fmall2203.comments.service.IProductCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2203.user.entity.Users;
import com.qf.fmall2203.user.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author CM
 * @since 2023-02-14
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ProductCommentsMapper productCommentsMapper;

    @Autowired
    IUsersService usersService;

    @Override
    public CommentsVo getComment(Integer Id) {

        Integer goods = productCommentsMapper.getComments();

        System.out.println(goods);

        Integer bad = productCommentsMapper.badComments();

        Integer middle = productCommentsMapper.middleComments();

        int total = (int) count(new QueryWrapper<ProductComments>(null));

        BigDecimal num1 = new BigDecimal(goods);
        BigDecimal num2 = new BigDecimal(total);

        double percent = num1.divide(num2,2,BigDecimal.ROUND_HALF_UP).doubleValue();

        CommentsVo commentsVo = new CommentsVo(goods, bad, middle, total, percent);

        return commentsVo;
    }

    @Override
    public Map<String,Object> getUserComment(Integer id, Integer pageNum, Integer limit) {


        HashMap<String, Object> map = new HashMap<>();

        Page<ProductComments> objectPage = new Page<>(pageNum,limit);

        Page<ProductComments> page = page(objectPage, null);

        List<ProductComments> list = page.getRecords();

        long total = page.getTotal();

        //List<ProductComments> list = list();

        for (ProductComments productComments : list) {

            String userId = productComments.getUserId();

            Users user = usersService.getById(userId);

            productComments.setUsername(user.getUsername());
            productComments.setNickname(user.getNickname());
            productComments.setUserImg(user.getUserImg());
        }

       // long total = objectPage.getTotal();

        map.put("pageCount",pageNum);
        map.put("count",total);
        map.put("list",list);
        return map;
    }
}
